<?php
namespace app\admin\controller;

use app\common\model\ViewBlock as ViewBlockModel;
use app\common\model\ViewBlockContent;
use think\Request;
use think\Session;

class ViewBlock extends Base
{
	protected $page_tag='view';
	protected $page_desc='前台页面模块显示内容管理';
	protected $page_title='页面内容';
	
	protected $sub_menu=[
		'index'=>[
			'title'=>'模块位置管理',
			'url'=>"/admin/view_block/index"
		]
	];
	public function index()
    {
	    $data['modify_url']=url("/admin/view_block/modifyBlock");
	    $data['detail_url']=url("/admin/view_block/content");
	    $vb=new ViewBlockModel();
	    $condition=[];
	    $data['title']=input('title');
	    $data['status']=input('status');
	    $data['title'] && $condition['title']=['like',"%".$data['title']."%"];
	    $data['status'] !==''  && input('status')!==null && $condition['status']=$data['status'];
	    $data['list']=$vb->getList($condition);
    	return $this->fetch('',$data);
    }
    
    public function modifyBlock(){
		$this->layout='layout_empty';
	    $data['submit_url']=url("/api/view_block/modifyBlock");
	    $data['status_list']=ViewBlockModel::$status;
	    $data['position_list']=ViewBlockModel::$position;
	    if(input('id')){
	    	$vb=new ViewBlockModel();
	    	$data['info']=$vb->getInfo(['id'=>input('id')]);
	    }
	    return $this->fetch('',$data);
    }
    
	public function content()
	{
		$vb_id=input('id');
		$data['modify_url']=url("/admin/view_block/modifyContent",['vb_id'=>$vb_id]);
		$data['del_url']=url("/api/view_block/delContent");
		$data['status_list']=ViewBlockContent::$status;
		$vbc=new ViewBlockContent();
		$condition=[];
		if($vb_id){
			$vb=new ViewBlockModel();
			$data['vb_title']=$vb->get(['id'=>$vb_id])['title'];
			$data['vb_id']=$vb_id;
			$condition['vb_id']=$vb_id;
			$this->sub_menu['content']=[
				'title'=>$data['vb_title'],
				'url'=>"/admin/view_block/content/id/".$vb_id
			];
		}
		input('title') && $condition['title']=['like',"%".input('title')."%"];
		input('status')!=='' && input('status')!==null   && $condition['status']=input('status');
		$data['list']=$vbc->getList($condition);
		return $this->fetch('',$data);
	}
	
	public function modifyContent(){
		$this->layout='layout_empty';
		$data['submit_url']=url("/api/view_block/modifyContent");
		$data['status_list']=ViewBlockContent::$status;
		$data['vb_id']=input('vb_id');
		$vb=new ViewBlockModel();
		$data['vb_list']=$vb->select();
		foreach ( $data['vb_list'] as &$item){
			$item['notice']=ViewBlockModel::$position[$item['position']]['notice'];
		}
		if(input('id')){
			$vbc=new ViewBlockContent();
			$data['info']=$vbc->getInfo(['id'=>input('id')]);
		}
		return $this->fetch('',$data);
	}
}
